//
//  AppDelegate.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//

#import "AppDelegate.h"
#import <AdjustSdk/AdjustSdk.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GlobalMethod writeBool:false local:LOCAL_HAVE_SHOW_APP_UPDATE];
    // 禁用黑暗模式
    if (@available(iOS 13.0, *)){
        self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    [self configAdjust];
    
    // 后台执行
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self requestGetDomain];
    });
    
    //createRootNav
    [GlobalMethod configRootNav];
    NSString * str = [UIPasteboard generalPasteboard].string;
    NSLog(@"%@",str);
    
    return YES;
}

// 域名检查
- (void)requestGetDomain{
    if ([[GlobalMethod getDeviceNetworkType] isEqualToString:@"none"]) {
        // 未连接网络
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self requestGetDomain]; // 再次调用该方法
        });
        return;
    }
    
    // 先请求IDFA权限，完成后再请求定位权限
    [self requestIDFAPermissionWithCompletion:^{
        // IDFA权限请求完成后，延迟请求定位权限
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[LocationService sharedInstance] startUpdatingLocation];
        });
    }];

    // 打点 - 第一次打开
    if ([GlobalMethod readBoolLocal:LOCAL_FIRST_OPEN] == false) {
        [GlobalMethod writeBool:YES local:LOCAL_FIRST_OPEN];
        [GlobalMethod hitPointWithPageName:KEY_Application action:KEY_First_Open extInfo:nil];
    }
}

// 配置 Adjust
- (void)configAdjust{
    NSString * adjustToken = @"colq2r83jjsw";
    NSString *environment = ADJEnvironmentSandbox;
#if DEBUG
    environment = ADJEnvironmentSandbox;
#else
    environment = ADJEnvironmentProduction;
#endif
    ADJConfig *adjustConfig = [[ADJConfig alloc] initWithAppToken:adjustToken environment:environment suppressLogLevel:true];
    [adjustConfig setLogLevel:ADJLogLevelSuppress];
    [Adjust initSdk:adjustConfig];
}

// 新增方法：专门处理IDFA权限请求
- (void)requestIDFAPermissionWithCompletion:(void(^)(void))completion {
    // 追踪权限
    NSString * adid = [GlobalMethod readUnprefixStrFromUserDefaults:LOCAL_ADID];
    if(!isStr(adid)){
        // 在主线程请求IDFA权限
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *advertisingIdentifier = [GlobalMethod getAdvertisingIdentifier];
            if(isStr(advertisingIdentifier)){
                NSRange adidRange = [advertisingIdentifier rangeOfString:@"0000-0000-0000"];
                if (adidRange.location != NSNotFound){
                    // 不是想要的格式
                }else{
                    [GlobalMethod writeUnprefixStr:advertisingIdentifier forKey:LOCAL_ADID];
                }
            }
            if (completion) {
                completion();
            }
        });
    } else {
        if (completion) {
            completion();
        }
    }
}

@end
